package com.example.rg1activitygameapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

public class StateActivity extends AppCompatActivity {

    private TextView textViewName;
    private TextView textViewPwd;
    private Button buttonBuy;
    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private ProgressBar progressBar3;
    private String weaponStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);
        textViewName=findViewById(R.id.textViewName);
        textViewPwd=findViewById(R.id.textViewPassword);
        Intent intent1=getIntent();
        textViewName.setText(intent1.getStringExtra("userName"));
        textViewPwd.setText(intent1.getStringExtra("userPwd"));
        buttonBuy=findViewById(R.id.buttonBuy);
        buttonBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(StateActivity.this,ShopActivity.class);
                startActivityForResult(intent,1);
            }
        });
        progressBar1=findViewById(R.id.progressBar1);
        progressBar2=findViewById(R.id.progressBar2);
        progressBar3=findViewById(R.id.progressBar3);

    }

    @Override
    public void onBackPressed() {
        UserInfo userInfo=new UserInfo(textViewName.getText().toString(),textViewPwd.getText().toString());
        Intent intent1=new Intent();
        intent1.putExtra("userInfo",userInfo);
        setResult(RESULT_OK,intent1);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        weaponStr=data.getStringExtra("weapon");
        Log.d("aaa",weaponStr);

        Gson gson=new Gson();
        Weapon weapon=gson.fromJson(weaponStr,Weapon.class);
        progressBar1.setProgress(progressBar1.getProgress()+weapon.getAttack());
        progressBar2.setProgress(progressBar2.getProgress()+weapon.getDefence());
        progressBar3.setProgress(progressBar3.getProgress()+weapon.getSpeed());
    }
}
